<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Product extends CI_Controller {
	public function __construct()
    {
        parent::__construct();
    }

	public function index($uri)
	{
		//$host = $this->db->select('id')->where('name',$_SERVER['HTTP_HOST'])->get('host')->row_array();
		//$host = $this->db->select('id')->where('name',$_SERVER['HTTP_HOST'])->get('host')->row_array();
		//if(!$host) return show_404();
		$this->db->select('id,title,price,theme,thumb,img_banners,content,tip,order_message,location_id,seo_keywords,seo_descript,price_base,fb_id,suit,attr,uri');
		//$this->db->where(['uri'=>$uri,'host_id'=>$host['id'],'statu'=>1]);
		$this->db->where(['uri'=>$uri,'statu'=>1]);
		$product = $this->db->get('product')->row_array();
		if(!$product) return show_404();
		$this->db->select('iso,currency.symbol as currency_symbol,currency.short as currency_short,lang.id');
		$this->db->join('currency', 'location.currency_id = currency.id');
		$this->db->join('lang', 'location.lang_id = lang.id');
        $location = $this->db->get('location')->row_array();
        $tran = getTran($location['id']);
        $product['path'] = '/theme/'.$product['theme'].'/';
        unset($location['id']);
        unset($product['theme']);
        $product = array_merge($product,$location);
        $this->load->view_path($product['path']);
        $this->load->view('index',[
        	'product' => $product,
        	'tran' => $tran
        ]);
	}

	public function buy($uri){
		$product = $this->db->get_where('product',['uri'=>$uri])->row_array();
        if(!$product) show_404();
	    $host = $this->db->get_where('host',['id'=>$product['host_id']])->row_array();
	    if($host['name']!=$_SERVER['HTTP_HOST']) show_404();
	    $currency = $this->db->get_where('currency',['id'=>$product['currency_id']])->row_array();
	    unset($product['currency_id']);
	    unset($product['host_id']);
        if(!$product) show_404();
        $product['attr'] = json_decode($product['attr'],true);
        $product['currency'] = $currency['symbol'];
        $this->show_product('buy',$product);
	}

	public function pay($uri){
		$buy_info = $this->input->get();
		$product = $this->db->get_where('product',['uri'=>$uri])->row_array();
        if(!$product) show_404();
	    $host = $this->db->get_where('host',['id'=>$product['host_id']])->row_array();
	    if($host['name']!=$_SERVER['HTTP_HOST']) show_404();
	    $currency = $this->db->get_where('currency',['id'=>$product['currency_id']])->row_array();
	    unset($product['currency_id']);
	    unset($product['host_id']);
        if(!$product) show_404();
        $product['attr'] = json_decode($product['attr'],true);
        $product['currency'] = $currency['symbol'];
        $product['buy_info'] = $buy_info;
        $attrs = explode(",",$buy_info['attrs']);
        $product['buy_info']['attrs'] = '';
        foreach($attrs as $key=>$attr){
        	$product['buy_info']['attrs'] .= $product['attr'][$key]['option'][$attr]['value'].';';
        }
        $this->show_product('pay',$product);
	}
	

}